import pymysql
class Student_system: #数据库操作类
	def __init__(self): #登入
		self.db = pymysql.connect(host="localhost", user="root", password="", database="blogdb")
		self.cursor = self.db.cursor()

	def find(self): #遍历全体
		self.cursor.execute("select * from stu")
		data = self.cursor.fetchall()
		print("所有学生信息：")
		for i in data:
			print("|{0:<5}| {1:<10}| {2:<5}| {3:<10}|".format(i[0],i[1],i[2],i[3]))

	def delete(self, id): #删除操作
		try:
			self.cursor.execute("delete from stu where id={}".format(id))
			self.db.commit()
			print("删除成功！")
		except :
			self.db.rollback()
			print("sql删除执行错误!")

	def insert(self, data): #插入操作
		try:
			self.cursor.execute("insert into stu(name,age,classid) value ({})".format(data))
			self.db.commit()
			print("添加成功！")
		except :
			self.db.rollback()
			print("sql添加执行错误!")


while True:
	Stu = Student_system()
	print("="*12,"学员管理系统","="*12)
	print("{0:1} {1:13} {2:15}".format(" ","1.查看学员信息","2.添加学员信息"))
	print("{0:1} {1:13} {2:15}".format(" ","3.删除学员信息","4.退出系统"))
	print("="*37)
	#判断模块
	key = input("请输入对应的选择：")

	if key == "1":

		print("="*12,"学员信息浏览","="*14)
		Stu.find()
		input("按回车键继续:")

	elif key == "2":

		print("="*12,"学员信息添加","="*14)
		name = input("请输入要添加的姓名:")
		age = input("请输入要添加的年龄:")
		classid = input("请输入要添加的班级:")
		data = '"{}",{},"{}"'.format(name, age, classid)
		Stu.insert(data)
		Stu.find()
		input("按回车键继续:")

	elif key == "3":
		print("="*12,"学员信息删除","="*14)
		Stu.find()
		sid = input("请输入你要删除的信息id:")
		Stu.delete(sid)
		Stu.find()
		input("按回车键继续:")

	elif key == "4":
		print("="*12,"再见","="*14)
		break;
	else:
		print("="*8," 无效的输入！ ","="*8)
db.close()
